#include"slist.h"
SListNode* BuySListNode(SLTDataType x)
{
	SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
	assert(newnode);//判断newnode是否malloc失败
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}

void SListPrint(SListNode* plist)
{
	//由于是一级指针，我这里试一下不创建临时变量看看会不会改变实参
	while (plist != NULL)
	{
		printf("%d->", plist->data);
		plist = plist->next;
	}
	printf("NULL\n");
}
void SListPushBack(SListNode** pplist, SLTDataType x)
{
	assert(pplist);
	SListNode* newnode = BuySListNode(x);
	if (*pplist == NULL)
	{
		*pplist = newnode;
	}
	else
	{
		SListNode* tail = *pplist;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}
void SListPushFront(SListNode** pplist, SLTDataType x)
{
	assert(pplist);
	SListNode* newnode = BuySListNode(x);
	newnode->next = *pplist;
	*pplist = newnode;
}
void SListPopBack(SListNode** pplist)
{
	assert(pplist);
	assert(*pplist);
	//删的只剩下一个的时候
	if ((*pplist)->next == NULL)
	{
		free(*pplist);
		*pplist = NULL;
	}
	else
	{
		SListNode* tailPre =NULL;
		SListNode* tail = *pplist;
		while (tail->next != NULL)
		{
			tailPre = tail;
			tail = tail->next;
		}
		free(tail);
		tailPre->next = NULL;
	}
}
void SListPopFront(SListNode** pplist)
{
	assert(pplist);
	assert(*pplist != NULL);
	SListNode* next = (*pplist)->next;
	free(*pplist);
	*pplist = next;
}
SListNode* SListFind(SListNode* plist, SLTDataType x)
{
	SListNode* cur = plist;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		else
		{
			cur = cur->next;
		}
	}
	return NULL;
}
void SListInsertAfter(SListNode** pos, SLTDataType x)
{
	assert(pos);
	SListNode* newnode = BuySListNode(x);
	SListNode* next = (*pos)->next;
	(*pos)->next = newnode;
	newnode->next = next;
}
void SListEraseAfter(SListNode** pos)
{
	assert(*pos);
	assert(pos);
	if ((*pos)->next == NULL)
	{
		return;
	}
	SListNode* del = (*pos)->next;
	(*pos)->next = del->next;

	free(del);
	del = NULL;
}
